/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package APP3.services;

import APP3.bookshop.book.ABook;
import APP3.bookshop.book.ElectronicBook;
import APP3.bookshop.book.PaperBook;
import APP3.bookshop.main.BookShop;
import APP3.bookshop.person.author.Author;
import APP3.bookshop.person.publisher.Publisher;
import java.util.Date;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.ListModel;

/**
 *
 * @author Asus
 */
public class BookManager {

    private BookShop bookShop;

    public BookManager(BookShop bookShop) {
        this.bookShop = bookShop;
    }

    public DefaultListModel populateBookList() {
        DefaultListModel bookList = new DefaultListModel();
        for (ABook book : bookShop.getLibrary()) {
            bookList.addElement(book);

        }

        return bookList;

    }

    public DefaultListModel populateBookAuthorsList(ABook b) {
        DefaultListModel bookList = new DefaultListModel();
        for (Author auth : b.getAuthor()) {
            bookList.addElement(auth);

        }

        return bookList;

    }

    public String addBook(String title, String url, Boolean ebook, Date publishingDate, Publisher pub, List<Author> authors) {
        if (!inputVerifier(title, url, ebook, publishingDate, pub, authors)) {
            return "INPUT ERROR";
        }

        if (ebook) {
            ElectronicBook book = new ElectronicBook(url, title, publishingDate, pub);
            for (int i = 0; i < authors.size(); i++) {
                book.getAuthor().add(authors.get(i));
                bookShop.getLibrary().add(book);
            }
        } else {
            PaperBook book = new PaperBook(title, publishingDate, pub);
            for (int i = 0; i < authors.size(); i++) {
                book.getAuthor().add(authors.get(i));
            }
            bookShop.getLibrary().add(book);
        }

        return "SAVED";
    }

    private boolean inputVerifier(String title, String url, Boolean ebook, Date publishingDate, Publisher pub, List<Author> authors) {
        if (authors.size() <= 0) {
            return false;
        }
        if (title.equals("") || ebook == null || publishingDate == null || pub == null) {
            return false;
        }

        return true;
    }

}
